<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<style>
    .fixed-table-body{
        height: calc(73vh);
    }

    .fixed-table-footer{
        border-top: 0;
    }
</style>
<body class="gray-bg">
<div class="">
    <div class="container-div">
        <div  id="flexSearchBox"  class="collapse out col-sm-12 search-box">
            <form id="user-form">
                <div class="select-list">
                    <ul>
                        <li class="time">
                            <label>Order Date:</label>
                            <input type="text" id="startTime" class="time-input reset"  placeholder="start date" name="beginOrderDate"/>
                            <span>to</span>
                            <input type="text" id="endTime" class="time-input reset"  placeholder="end date" name="endOrderDate"/>
                        </li>
                        <li>
                            Order ID：<input class="reset" type="text" name="orderID"/>
                        </li>
                        <li>
                            SKU：<input class="reset" type="text" name="unitSKUs"/>
                        </li>
                        <li class="time">
                            <label>Update Date:</label>
                            <input type="text" id="startTime1" class="time-input reset"  placeholder="start date" name="beginConfirmedDate"/>
                            <span>to</span>
                            <input type="text" id="endTime1" class="time-input reset"  placeholder="end date" name="endConfirmedDate"/>
                        </li>
                        <li>
                            AWB：<input class="reset" type="text" name="aWB"/>
                        </li>
                        <li shiro:hasPermission="module:awaitingAction:companySearch">
                            company：<select class="reset_select" id="selCompanyId" name="companyID" >
                            <option th:each="dep : ${depList}" th:text="${dep.deptName}"
                                    th:value="${dep.deptId}"></option>
                        </select>
                        </li>
                        <li>
                            <a class="btn btn-success btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;search</a>
                            <a class="btn btn-primary btn-rounded btn-sm reset-booton"><i class="glyphicon glyphicon-repeat"></i>&nbsp;reset</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group hidden-xs" id="toolbar" role="group">
            <!--<a class="btn btn-outline btn-success btn-rounded" onclick="$.operate.add()" shiro:hasPermission="module:awaitingAction:add">
                <i class="fa fa-plus"></i> add
            </a>-->
            <a class="btn btn-outline btn-danger btn-rounded" onclick="batchDeleteRemark()" shiro:hasPermission="module:ordersDetail:remove">
                <i class="fa fa-trash-o"></i> batch delete
            </a>
            <a class="btn btn-outline btn-info" onclick="batchPrintBillAll()" >
                <i class="fa fa-print"></i> batch print
            </a>
            <a class="btn btn-outline btn-success btn-rounded" onclick="batchSendOut('sendout')" shiro:hasPermission="module:awaitingAction:sendout">
                <i class="fa fa-print"></i> batch sendOut
            </a>
            <a class="btn btn-outline btn-success btn-rounded"  onclick="sweepCodeDelivery('AwaitingAction 扫码发货')" shiro:hasPermission="module:awaitingAction:sweepCodeDelivery">
                <i class="fa fa-print"></i> scan sendOut
            </a>
            <a class="btn btn-outline btn-info btn-rounded flex-botton" data-toggle="collapse" data-target="#flexSearchBox">
                <i class="glyphicon glyphicon-chevron-down">unfoldSearch
                </i>
            </a>
        </div>

        <div class="col-sm-12 select-info table-responsive">
            <table id="bootstrap-table" class="table text-nowrap" data-height="770" data-mobile-responsive="true"></table>
        </div>

        <div  id="batchPrint_div" style="">
            <img src='' class='img-responsive' id='batchPrintBill' />
            <div style='page-break-after:always;'></div>
            <img src='' class='img-responsive' id="batchPrintFaceBill" />
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var faceOrderFlag = [[${@permission.hasPermi('module:ordersDetail:faceOrder')}]];
    var removeFlag = [[${@permission.hasPermi('module:awaitingAction:remove')}]];
    var uploadFlag = [[${@permission.hasPermi('module:awaitingAction:upload')}]];
    var sendoutFlag = [[${@permission.hasPermi('module:awaitingAction:sendout')}]];
    var reminderFlag = [[${@permission.hasPermi('module:awaitingAction:reminder')}]];
    var prefix = ctx + "module/awaitingAction";
    var orderStateDict = [[${@dict.getType('erp_order_status')}]];//订单状态value转label
    var companyDict = [[${@dict.getType('erp_company')}]];//公司id转name

    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            modalName: "订单详情",
            showFooter:true,
            rowStyle:function (row, index){
                if(row.isUrge==1){
                    return { classes:"reminder"};
                }else{
                    return {classes:""};
                }
            },
            columns: [{
                checkbox: true
            },
                {
                    field : 'id',
                    title : 'ID',
                    visible: false
                },
                {
                    title: 'serialNumber',

                    formatter: function (value, row, index) {
                        var pageSize = $('#bootstrap-table').bootstrapTable('getOptions').pageSize;     //通过table的#id 得到每页多少条
                        var pageNumber = $('#bootstrap-table').bootstrapTable('getOptions').pageNumber; //通过table的#id 得到当前第几页
                        return pageSize * (pageNumber - 1) + index + 1;    // 返回每条的序号： 每页条数 *（当前页 - 1 ）+ 序号
                    },
                    footerFormatter: function (value) {
                        return 'total';
                    }
                },
                {
                    title: 'operation',
                    clickToSelect : false,
                    formatter: function(value, row, index) {
                        var actions = [];
                        //actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>edit</a> ');
                        if((row.surfaceBillUrl!='' && row.surfaceBillUrl!=null) && (row.sendBillUrl!='' && row.sendBillUrl!=null)){
                            actions.push('<a class="btn btn-warning btn-xs ' + reminderFlag + '" href="#" onclick="reminder(\'' + row.id + '\')"><i class="fa fa-at"></i>reminder</a>');
                        }
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="#" onclick="deleteRemark(\''+row.id+'\')"><i class="fa fa-remove"></i>delete</a>');
                        // actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="#" onclick="selectMianDan(\'' + row.id + '\')"><i class="fa fa-search"></i>面单</a> ');
                        actions.push('<a class="btn btn-info btn-xs ' + faceOrderFlag + '" href="#" onclick="uploadModal(\'' + row.id + '\')"><i class="fa fa-upload"></i><bills></bills></a> ');
                        if((row.surfaceBillUrl!='' && row.surfaceBillUrl!=null) && (row.sendBillUrl!='' && row.sendBillUrl!=null)){
                            actions.push('<a class="btn btn-success btn-xs ' + sendoutFlag + '" href="#" onclick="sendOut(\'' + row.id + '\')"><i class="fa fa-truck"></i>sendOut</a> ');
                        }
                        return actions.join('');
                    }
                },
                {
                    field : 'surfaceBillPrint',
                    title : 'surfaceBillPrint',

                    sortable: true,
                    formatter: function(value,row,index){
                        var size = 'fa-lg';
                        value = '<i class="fa fa-check '+size+'" style="color: #b7b7b7;font-size: 28px"></i>';
                        if ( row.surfaceBillPrint == 1 ){
                            value = '<i class="fa fa-check '+size+'" style="color: red;font-size: 28px"></i>';
                            return value;
                        }
                        return value;
                    }
                },
                {
                    field : 'sendBillPrint',
                    title : 'sendBillPrint',

                    sortable: true,
                    formatter: function(value,row,index){
                        var size = 'fa-lg';
                        value = '<i class="fa fa-check '+size+'" style="color: #b7b7b7;font-size: 28px"></i>';
                        if ( row.sendBillPrint == 1 ){
                            value = '<i class="fa fa-check '+size+'" style="color: red;font-size: 28px"></i>';
                            return value;
                        }
                        return value;
                    }
                },
                {
                    field : 'orderDate',
                    title : 'Order Date',
                    sortable: true,
                    formatter:function (value, row, index) {
                        if(row.isUrge==1){
                            return "<span class='remindering'>"+value+"</span>";
                        }else{
                            return value;
                        }
                    }

                },
                {
                    field:'orderID',//订单id
                    title:'<span class="globalSearch_column">Order Id</span>',
                    sortable: true,
                },
                {
                    field : 'orderStatus',//订单状态
                    title : 'Order Status',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(orderStateDict, value);
                    }
                },
                {
                    field : 'lastStatus',//订单上个状态
                    title : 'Last Status',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(orderStateDict, value);
                    }
                },
                {
                    field : 'ofItems',//商品数量
                    title : '# ofItems',
                    sortable: true,
                    footerFormatter: function (value) {
                        return calculateLineTotalAsServer(value,$.table.sumRows.ofItems);
                    }
                },
                {
                    field : 'totalHandlingTime',//处理时长
                    title : 'Total Handing Time',
                    sortable: true,
                },
                {
                    field : 'awb',//航空货运单
                    title : 'AWB'
                },
                {
                    field : 'courier',//快递
                    title : 'Courier'
                },
                {
                    field : 'itemCollectionType',//商品类型
                    title : 'Item Collection Type',
                    sortable: true,
                },
                {
                    field : 'totalShippingFees',//总运送费
                    title : 'Total Shipping Fees',
                    sortable: true,
                    footerFormatter: function (value) {
                        return calculateLineTotalAsServer(value,$.table.sumRows.totalShippingFees);
                    }
                },
                {
                    field : 'paidBySeller',//卖家付款
                    title : 'Paid By Seller',
                    sortable: true,
                    footerFormatter: function (value) {
                        return calculateLineTotalAsServer(value,$.table.sumRows.paidBySeller);
                    }
                },
                {
                    field : 'paymentMethod',//支付方式
                    title : 'Payment Method',
                    sortable: true,

                },
                {
                    field : 'codfee',//货到付款费用
                    title : 'Cod Fee',
                    sortable: true,
                    footerFormatter: function (value) {
                        return calculateLineTotalAsServer(value,$.table.sumRows.codfee);
                    }
                },
                {
                    field : 'isPaid',//是否支付
                    title : 'Is Paid',
                    sortable: true,
                },
                {
                    field : 'paymentReleaseDate',//付款日期
                    title : 'Payment Release Date',
                    sortable: true,
                },
                {
                    field : 'confirmedDate',//确认日期
                    title : 'Confirmed Date',
                    sortable: true,
                },
                {
                    field : 'shippedDate',//发货日期
                    title : 'Shipped Date',
                    sortable: true,
                },
                {
                    field : 'deliveryDate',//交货日期
                    title : 'Delivery Date',
                    sortable: true,
                },
                {
                    field : 'orderAge',//订单耗费时长
                    title : 'Order Age',
                    sortable: true,
                },
                {
                    field : 'shippedAge',//发货耗费时长
                    title : 'Shipped Age',
                    sortable: true,
                },
                {
                    field : 'deliveryAge',//交货耗费时长
                    title : 'Delivery Age',
                    sortable: true,
                },
                {
                    field : 'grandTotal',//总计
                    title : 'Grand Total',
                    sortable: true,
                    footerFormatter: function (value) {
                        return calculateLineTotalAsServer(value,$.table.sumRows.grandTotal);
                    }
                },
                {
                    field : 'unitSKUs',
                    title:'<span class="globalSearch_column">unitSKUs</span>',
                },
                {
                    field:'updateDate',//修改时间（缺省上传时间）
                    title:'<span class="globalSearch_column">lastOperationTime </span>',
                    sortable: true,
                },
                {
                    field : 'companyName',
                    title : 'company',
                },
            ],
            onLoadSuccess: function (item, $element) {
                $('.fixed-table-footer').addClass("totalRowBackground-color");//合计行添加颜色
            },
        };
        $.table.init(options);
    });
</script>
<style>
    .fixed-table-body{
        height: calc(73vh);
    }

    .fixed-table-footer{
        border-top: 0;
    }
</style>
<script th:src="@{/ruoyi/js/common.js}"></script>
<script th:src="@{/ajax/libs/jquery-migrate/jQuery.print.min.js}"></script>
<script>



    //成功回调函数
    $("#bootstrap-table").on('load-success.bs.table',function(data){
        /*催促单置顶变色*/
        var urge_tr = $(".remindering").parents("tr");//催促订单的行
        urge_tr.css("background-color","rgba(216,54,54,0.2)");//催促单变红色
    });



    //上传面单（弹出层）
    function uploadModal(id){
        var url = prefix+"/uploadModal/"+id;
        $.modal.open("添加" + $.table._option.modalName, url);
    }

    /**
     * 催单
     * @param id
     */
    function reminder(id) {
        $.modal.confirm('Are you sure you want reminder？',function () {
            $.ajax({
                url: prefix+'/reminder',
                dataType: 'json',
                type: 'post',
                data: {'id':id},
                success: function (data) {
                    $.table.refresh();
                },
                error: function (data) {

                }
            })
        })
    }


    /**
     * 批量打印面单收货单
     */
    function batchPrintBillAll() {
        var ids = [];//选中行id数组
        //选中行数据
        var getSelectRows = $("#bootstrap-table").bootstrapTable('getSelections', function (row) {
            return row;
        });
        for (var i = 0;i<getSelectRows.length;i++){
            ids.push(getSelectRows[i].id);
            if(getSelectRows[i].surfaceBillUrl==""||getSelectRows[i].surfaceBillUrl==null||getSelectRows[i].sendBillUrl==""||getSelectRows[i].sendBillUrl==null){
                $.modal.msg("bills include unuploaded surfaceBill or sendBill！！");
                return false;
            }
        }
        if(ids.length<=0){
            $.modal.msg("Please select at least one record！");
            return false;
        }

        var url = prefix+"/toPrintPreview?ids="+ids;
        $.modal.open("<h3 style='margin-top: 12px'>batch print preview</h3>", url);

    }

    //发货
    function sendOut(id) {
        $.modal.confirm('Are you sure about send out？',function () {
            $.ajax({
                url: prefix+'/sendOut',
                dataType: 'json',
                type: 'post',
                data: {'id':id},
                success: function (data) {
                    if(data.length>0){
                        // $.modal.alertWarning("发货成功，商品不存在！");
                        $.table.refresh();
                    }else{
                        $.table.refresh();
                    }
                },
                error: function (data) {

                }
            })
        })
    }

    //批量发货/批量删除
    function batchSendOut(sign,id) {//操作标记，单行操作时的id值
        var tips ;//操作提示
        //获取选中行
        var getSelectRows = $("#bootstrap-table").bootstrapTable('getSelections', function (row) {
            return row;
        });

        if(getSelectRows.length==0&&id==undefined){
            $.modal.msg("Please select at least one record！");
            return false;
        }

        var ids = [];//选中行的id数组

        if (id!=undefined &&id!=null){//id有值为单行操作，否则为批量
            ids.push(id);
        }else {
            for (var i =0;i<getSelectRows.length;i++){
                ids.push(getSelectRows[i].id);
            }
        }

        if(sign=="delete"){
            if(id!=undefined){
                tips="Are you sure you want to delete it？";
            }else{
                tips="Are you sure you want to delete the"+ getSelectRows.length + "selected data?";
            }
        }else if(sign=="sendout"){
            if(id!=undefined){
                tips="Are you sure about send out？";
            }else{
                tips="Are you sure about the "+ getSelectRows.length +" data selected in the send out？";
            }
        }

        $.modal.confirm(tips, function() {
            $.ajax({
                url:prefix+'/batchUpdateByPrint',
                dataType:'json',
                type:'post',
                data:{
                    ids:ids,
                    sign:sign
                },
                success:function (data) {
                    console.log(data);
                    var notExistGoods="";//发货中不存在的商品的位置

                    if(data.length>0){
                        for (var i = 0;i<data.length;i++){
                            if (i!=data.length-1){
                                notExistGoods+=data[i]+"、";
                            }else {
                                notExistGoods+=data[i];
                            }
                        }
                        // $.modal.alertWarning("发货成功，其中有"+data.length+"件商品不存在！");
                        console.log(notExistGoods);
                        $.table.refresh();
                    }else{
                        $.table.refresh();
                    }
                },
                complete: function(XMLHttpRequest, textStatus) {
                }
            });
        });
    }

    //扫码发货弹出层
    function sweepCodeDelivery(title,url){
        var sign = title.split(" ");
        var url = prefix+"/toSweepCodeDelivery?flag="+sign[0];
        $.modal.open("<h3 style='margin-top: 10px'>"+title+"</h3>", url,"",400);
    }


    //删除备注弹出层
    function deleteRemark(id) {
        var url = prefix+"/deletePopup?id="+id;
        $.modal.open("<h3 style='margin-top: 12px'>Delete order remark</h3>", url,400,300);
    }

    //删除备注弹出层
    function batchDeleteRemark() {
        var getSelectRows = $("#bootstrap-table").bootstrapTable('getSelections', function (row) {
            return row;
        });
        if(getSelectRows.length<1){
            $.modal.msg("Please select at least one record！");
            return false;
        }
        var ids = [];//选中行的id数组
        for (var i =0;i<getSelectRows.length;i++){
            ids.push(getSelectRows[i].id);
        }
        var url = prefix+"/batchDeletePopup?ids="+ids;
        $.modal.open("<h3 style='margin-top: 12px'>Batch delete order remark</h3>", url,400,300);
    }
</script>
</body>
</html>